package com.beetlestech.nfe.main.biz.com.impl;

import java.util.UUID;

import javax.mail.MessagingException;

import com.beetlestech.nfe.fw.core.bean.BeanFactory;
import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.config.SystemContext;
import com.beetlestech.nfe.fw.core.properties.MessageProperties;
import com.beetlestech.nfe.main.biz.com.HbEmailPwdRecover;
import com.beetlestech.nfe.main.biz.com.util.SendEmail;
import com.beetlestech.nfe.main.biz.constant.CommonConfigConstant;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.EmailactiveDTO;
import com.beetlestech.nfe.main.db.tbl.MstusrDTO;

public class HbEmailPwdRecoverImpl extends AbstractDBLogic implements HbEmailPwdRecover, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {

	public void sendMailForRecoverPwd(String email, String serverURL, Integer usrid) {

		MstusrDTO mstusrDTO =new MstusrDTO(); 
		mstusrDTO.setusrid(usrid);
		mstusrDTO.setstate(0);
		updateDAO.updateOne(MstusrDTO.UPDATE, mstusrDTO, M10040WS);

		//激活码生成
		UUID uuid = UUID.randomUUID();
	    String activeCode =uuid.toString();

	    EmailactiveDTO emailactiveDTO=new EmailactiveDTO();
	    emailactiveDTO.setactiveCode(activeCode);
	    emailactiveDTO.setuserid(mstusrDTO.getusrid());
	    updateDAO.updateOne(EmailactiveDTO.INSERT, emailactiveDTO, M10040WS);

	    BeanFactory beanFactory = SystemContext.getBeanFactory();
	    String serverContextURL = null;
	    try {
			MessageProperties resourcesProperties = beanFactory.beanOfName(CommonConfigConstant.COMMON_CONFIG_FILENAME);
			serverContextURL = resourcesProperties.get(CommonConfigConstant.SERVER_CONTEXT_URL);
		} catch (Exception e) {
			e.printStackTrace();
			createApplicationException("M10440WS", "服务器URL");
		}
	    
	    //mail content 作成
	    StringBuffer sb=new StringBuffer("点击下面链接激活账号, 请尽快激活！</br>");
        sb.append("<a href=\"" + serverContextURL + "recoverFromMailAction.do?&activeCode=");
        sb.append(activeCode);
        sb.append("\">" + serverContextURL + "recoverFromMailAction.do?&activeCode=");
        sb.append(activeCode);
        sb.append("</a>");

        try {
			SendEmail.sendEmail(email, sb.toString());
		} catch (MessagingException e) {
			createApplicationException("M10101WC", "邮件");
		}
	}

	
	
}
